<template>
  <div class="app-container">
    <div class="filter-container">
      <div
        class="float-right"
        v-show="permission_roles.fetchEquipmentUnbindData"
      >
        <el-button
          type="danger"
          class="send_new_btn"
          @click="removeMac()"
        >解除绑定</el-button>
      </div>
      <div class="clearfix"></div>
    </div>
    <div class="table_container">
      <el-table
        border
        v-loading="tableloading"
        :data="equipmentBindData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          prop="hardwareEquipment"
          label="蓝牙地址"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="userId"
          label="用户ID"
          align="center"
        ></el-table-column>
        <el-table-column
          label="操作内容"
          align="center"
        >
          <template slot-scope="scope">
            <span
              class="lz-span lz-success"
              v-if="scope.row.status == '1'"
            >绑定</span>
            <span
              class="lz-span lz-danger"
              v-else
            >解绑</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="unbandTime"
          :formatter="formatterTime"
          label="操作时间"
          align="center"
          width="100"
        ></el-table-column>
      </el-table>

      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
  </div>

</template>
<script>
import { mapGetters } from "vuex";
import {
  fetchGetEquipmentHistoryData,
  fetchEquipmentUnbindData
} from "@/api/table";
import { formatDateTime } from "@/utils/common";
export default {
  data() {
    return {
      count: 0,
      limit: 20,
      currentPage: 1,
      tableloading: false,
      searchInpt: "",
      equipmentBindData: [],
      userId: ""
    };
  },
  mounted() {
    this.getEquipmentHistoryData();
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getEquipmentHistoryData();
    },
    formatterTime(row) {
      if (row.bandingTime) {
        return formatDateTime(row.bandingTime);
      } else if (row.unbandTime) {
        return formatDateTime(row.unbandTime);
      } else {
        return "";
      }
    },
    removeMac() {
      this.$confirm("解除设备, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delMac();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    async delMac() {
      const res = await fetchEquipmentUnbindData({
        hardwareEquipment: this.$route.query.hardwareEquipment
      });
      if (res.status == 200) {
        this.getEquipmentHistoryData();
        this.$message({
          type: "success",
          message: "解绑成功!"
        });
      } else {
        this.$message({
          type: "error",
          message: res.msg
        });
      }
    },
    async getEquipmentHistoryData() {
      this.tableloading = true;
      const manage = await fetchGetEquipmentHistoryData({
        pageSize: this.limit,
        pageNo: this.currentPage,
        hardwareEquipment: this.$route.query.hardwareEquipment
      });
      if (manage.data.list) {
        this.tableloading = false;
        this.equipmentBindData = [];
        this.count = manage.data.total;
        manage.data.list.forEach(item => {
          const manageTable = {};
          manageTable.bandingTime = item.bandingTime;
          manageTable.unbandTime = item.unbandTime;
          manageTable.hardwareEquipment = item.hardwareEquipment;
          manageTable.nickName = item.nickName;
          manageTable.userId = item.userId;
          manageTable.username = item.username;
          manageTable.status = item.status;
          this.equipmentBindData.push(manageTable);
        });
      }
    }
  },
  computed: {
    ...mapGetters(["permission_roles"])
  },
  watch: {
    $route() {
      this.getEquipmentHistoryData();
    }
  }
};
</script>
<style lang="less" scoped>
.search_box {
  padding: 20px 0;
}
.last {
  text-align: right;
}
</style>

